package com.community.common.controller;

/**
 * Created by Enzo Cotter on 2020/10/18.
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.community.common.mapper.FriendHouseMapper;
import com.community.common.pojo.PageRequest;
import com.community.common.pojo.domain.FriendHouse;
import com.community.common.pojo.domain.OwnerFriend;
import com.community.common.pojo.dto.FriendUserCondition;
import com.community.common.pojo.dto.PublicUserCondition;
import com.community.common.pojo.response.CodeEnum;
import com.community.common.pojo.response.LayuiPage;
import com.community.common.pojo.response.MyResponse;
import com.community.common.pojo.vo.FriendUserVo;
import com.community.common.pojo.vo.OwnFriendVo;
import com.community.common.pojo.vo.OwnerCommunityVo;
import com.community.common.pojo.vo.PublicUserVo;
import com.community.common.service.ManagerUserService;
import com.community.common.service.OwnerFriendService;
import com.community.common.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 好友信息
 */
@Slf4j
@RestController
@RequestMapping("/friend")
public class OwnerFriendController {

    @Resource
    private OwnerFriendService ownerFriendService;
    @Resource
    private ManagerUserService managerUserService;
    /**
     * 根据id查询用户好友列表
     */
    @GetMapping("/listId")
    public MyResponse list(String openId) {
        LambdaQueryWrapper<OwnerFriend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OwnerFriend::getOpenId, openId).eq(OwnerFriend::getIsDelete, 0);
        List<OwnerFriend> list = ownerFriendService.list(queryWrapper);
        return MyResponse.success(list);
    }
    /**
     * 根据条件查询用户好友列表
     */
    @PostMapping("/list")
    public LayuiPage list(@RequestBody PageRequest<FriendUserCondition> friendUserConditionPageRequest, HttpSession session) {
        List<FriendUserVo> friendUserVos = ownerFriendService.queryByPageAll(friendUserConditionPageRequest,
                managerUserService.getCommunityIdFromSession(session), "0");
        return LayuiPage.parsePage(friendUserVos);
    }
    /**
     * 根据id获取好友信息
     */
    @GetMapping("/info")
    public MyResponse info(String id) {
        OwnFriendVo ownFriendVo = ownerFriendService.info(id);
        if (ownFriendVo == null) {
            return MyResponse.fail(CodeEnum.NOT_FOUND);
        }
        return MyResponse.success(ownFriendVo);
    }

    /**
     * 新增或修改用户好友
     */
    @PostMapping("/save")
    public MyResponse save(@RequestBody OwnerFriend ownerFriend) {
        return ownerFriendService.saveOrUpdateOwnerFriend(ownerFriend);
    }

    /**
     * 根据id删除好友
     */
    @GetMapping("/delete")
    public MyResponse delete(Long id) {
        ownerFriendService.delete(id);
        return MyResponse.success("删除好友成功");
    }

    @Resource
    private FriendHouseMapper friendHouseMapper;

    /**
     * 好友添加小区
     */
    @ResponseBody
    @PostMapping("/house")
    public MyResponse userCommunityAdd(@RequestBody FriendHouse friendHouse) {
        friendHouse.setId(CommonUtil.getIdStr()).setCreateTime(LocalDateTime.now());
        friendHouseMapper.insert(friendHouse);
        return MyResponse.success();
    }

    /**
     * 好友删除小区
     */
    @ResponseBody
    @DeleteMapping("/house")
    public MyResponse userCommunityDel(@RequestBody FriendHouse friendHouse) {
        friendHouseMapper.deleteById(friendHouse);
        return MyResponse.success();
    }

    @ResponseBody
    @GetMapping("/house/{friendId}")
    public MyResponse userCommunityQuery(@PathVariable String friendId) {
        List<OwnerCommunityVo> list = friendHouseMapper.userCommunityQuery(friendId);
        return MyResponse.success(list);
    }

}
